package zengjin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import zengjin.common.PageResponse;
import zengjin.mapper.*;
import zengjin.model.dto.offer.AddOfferRequest;
import zengjin.model.dto.offer.PageOfferRequest;
import zengjin.model.dto.offer.UpdateOfferRequest;
import zengjin.model.en.*;
import zengjin.model.vo.offer.OfferDetailVO;
import zengjin.model.vo.offer.PageOfferVO;
import zengjin.service.OfferService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 19324
 * @description 针对表【offer】的数据库操作Service实现
 * @createDate 2025-03-04 10:00:12
 */
@Service
public class OfferServiceImpl extends ServiceImpl<OfferMapper, Offer>
        implements OfferService {

    @Resource
    private OfferMapper offerMapper;

    @Resource
    private EmployerMapper employerMapper;

    @Resource
    private ResumeMapper resumeMapper;

    @Resource
    private ImageMapper imageMapper;

    @Resource
    private JobMapper jobMapper;

    @Override
    public Boolean addOffer(AddOfferRequest addOfferRequest) {
        //1.获取参数
        Long offerJobseekerId = addOfferRequest.getOfferJobseekerId();
        Long offerEmployerId = addOfferRequest.getOfferEmployerId();
        Long offerJobId = addOfferRequest.getOfferJobId();
        String offerStatus = addOfferRequest.getOfferStatus();
        String offerRemarks = addOfferRequest.getOfferRemarks();
        Date offerEndTime = addOfferRequest.getOfferEndTime();

        //2.调用mapper
        Offer offer = new Offer();
        offer.setOfferJobseekerId(offerJobseekerId);
        offer.setOfferEmployerId(offerEmployerId);
        offer.setOfferJobId(offerJobId);
        offer.setOfferStatus(offerStatus);
        offer.setOfferRemarks(offerRemarks);
        offer.setOfferEndTime(offerEndTime);

        return offerMapper.insert(offer) > 0;
    }

    @Override
    public Boolean updateOfferStatus(Long offerId, String offerStatus) {
        UpdateWrapper<Offer> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("offer_id", offerId)
                .set("offer_status", offerStatus);
        return offerMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public Boolean updateOffer(UpdateOfferRequest updateOfferRequest) {
        //1.获取参数
        Long offerId = updateOfferRequest.getOfferId();
        String offerStatus = updateOfferRequest.getOfferStatus();
        String offerRemarks = updateOfferRequest.getOfferRemarks();
        Date offerEndTime = updateOfferRequest.getOfferEndTime();
        //2.调用mapper
        LambdaUpdateWrapper<Offer> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Offer::getOfferId, offerId)
                .set(StrUtil.isNotBlank(offerStatus), Offer::getOfferStatus, offerStatus)
                .set(StrUtil.isNotBlank(offerRemarks), Offer::getOfferRemarks, offerRemarks)
                .set(offerEndTime != null, Offer::getOfferEndTime, offerEndTime);
        return offerMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public PageResponse<PageOfferVO> pageOffer(PageOfferRequest pageOfferRequest) {
        //1.获取参数
        Long offerJobseekerId = pageOfferRequest.getOfferJobseekerId();
        Long offerEmployerId = pageOfferRequest.getOfferEmployerId();

        List<PageOfferVO> pageOfferVOS = new ArrayList<>();
        //2.判断查询对象
        QueryWrapper<Offer> queryWrapper = new QueryWrapper<>();
        if (offerJobseekerId != null) {
            //求职者查询
            //查询求职者的所有offer
            queryWrapper.eq("offer_jobseeker_id", offerJobseekerId);
            List<Offer> offers = offerMapper.selectList(queryWrapper);
            offers.forEach(offer -> {
                //封装Offer信息
                PageOfferVO pageOfferVO = new PageOfferVO();
                BeanUtil.copyProperties(offer, pageOfferVO);
                //封装企业信息
                Employer employer = employerMapper.selectById(offer.getOfferEmployerId());
                BeanUtil.copyProperties(employer, pageOfferVO);
                QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
                imageQueryWrapper.eq("image_name_id", employer.getEmployerId());
                imageQueryWrapper.eq("image_name", "employer_avatar");
                Image image = imageMapper.selectOne(imageQueryWrapper);
                pageOfferVO.setUserAvatarUrl((image.getImageUrl()));
                //封装岗位信息
                Job job = jobMapper.selectById(offer.getOfferJobId());
                BeanUtil.copyProperties(job, pageOfferVO);
                pageOfferVOS.add(pageOfferVO);
            });
        } else {
            //企业查询
            //查询企业发出的所有Offer
            queryWrapper.eq("offer_employer_id", offerEmployerId);
            List<Offer> offers = offerMapper.selectList(queryWrapper);
            offers.forEach(offer -> {
                //封装Offer信息
                PageOfferVO pageOfferVO = new PageOfferVO();
                BeanUtil.copyProperties(offer, pageOfferVO);
                //求职者信息
                QueryWrapper<Resume> resumeQueryWrapper = new QueryWrapper<>();
                resumeQueryWrapper.eq("resume_jobseeker_id", offer.getOfferJobseekerId());
                Resume resume = resumeMapper.selectOne(resumeQueryWrapper);
                BeanUtil.copyProperties(resume, pageOfferVO);
                QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
                imageQueryWrapper.eq("image_name_id", resume.getResumeId());
                imageQueryWrapper.eq("image_name", "resume_avatar");
                Image image = imageMapper.selectOne(imageQueryWrapper);
                pageOfferVO.setUserAvatarUrl((image.getImageUrl()));
                //封装岗位信息
                Job job = jobMapper.selectById(offer.getOfferJobId());
                BeanUtil.copyProperties(job, pageOfferVO);
                pageOfferVOS.add(pageOfferVO);
            });
        }
        //3.构建分页条件
        Page<PageOfferVO> page = pageOfferRequest.toPageDefaultSortByUpdateTime();
        return PageResponse.of(page, pageOfferVOS);

    }

    @Override
    public OfferDetailVO getOfferDetailById(Long offerId) {
        OfferDetailVO offerDetailVO = new OfferDetailVO();
        //1.查询出offer
        Offer offer = offerMapper.selectById(offerId);
        BeanUtil.copyProperties(offer, offerDetailVO);
        //2.查询出企业信息
        Employer employer = employerMapper.selectById(offer.getOfferEmployerId());
        BeanUtil.copyProperties(employer, offerDetailVO);
        QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("image_name_id", employer.getEmployerId());
        imageQueryWrapper.eq("image_name", "employer_avatar");
        Image image = imageMapper.selectOne(imageQueryWrapper);
        offerDetailVO.setUserAvatarUrl((image.getImageUrl()));
        //3.查询出岗位信息
        Job job = jobMapper.selectById(offer.getOfferJobId());
        BeanUtil.copyProperties(job, offerDetailVO);
        return offerDetailVO;
    }
}




